import { IMetaData } from './type';
import { _processArrayToTree } from './utils';
import MetaData from './metadata';

export default class RzMetaData {
  private schemas: IMetaData[];

  constructor(props: IMetaData[]) {
    this.schemas = props;
  }

  private static _instance: RzMetaData | null = null;

  /**
   * 获取类的实例
   * @param mataDataOptions
   * @returns
   */
  public static getInstance(mataDataOptions?: IMetaData[]): RzMetaData {
    if (RzMetaData._instance === null && mataDataOptions) {
      RzMetaData._instance = new RzMetaData(mataDataOptions);
    }
    return RzMetaData._instance!;
  }

  /**
   * 初始化元数据的实例
   */
  initSchema(): any {
    if (this.schemas) {
      // 将扁平化的数据结构转化成树状对象
      const mianSchema = _processArrayToTree(this.schemas);
      console.log('mianSchema', mianSchema);

      const mainInstance = MetaData.getInstance(mianSchema);
      console.log('mainInstance', mainInstance);
      return mainInstance.initSchema();
    }
    return '暂无元数据schema';
  }

  toString(obj: any) {
    return JSON.stringify(obj);
  }
}
